package com.alipay.app.groupmeal.standard.ui.b.function.mode;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;

import androidx.annotation.Nullable;

import com.alipay.app.groupmeal.standard.R;
import com.alipay.app.groupmeal.standard.http.bean.resp.Result;
import com.alipay.app.groupmeal.standard.offline.bean.FkBean;
import com.alipay.app.groupmeal.standard.offline.bean.OrderBean;
import com.alipay.app.groupmeal.standard.offline.bean.SbruanDinnerBean;
import com.alipay.app.groupmeal.standard.offline.manager.FkBeanManager;
import com.alipay.app.groupmeal.standard.offline.manager.SbruanDinnerDataManager;
import com.alipay.app.groupmeal.standard.ui.view.WaveProgressView;
import com.alipay.app.groupmeal.standard.ui.b.util.SysHttpUtil;
import com.alipay.app.groupmeal.standard.widget.business.BScreenRecyclerView;
import com.alipay.app.groupmeal.standard.widget.business.BScreenTitleView;
import com.alipay.app.groupmeal.standard.widget.business.BaseBScreenView;
import com.alipay.app.groupmeal.standard.widget.business.BottomSnView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.internal.observers.BlockingBaseObserver;

/**
 * 本页为白名单下载，其中包括机号表，餐次表，卡类表，机器基本设置
 */
public class BScreenDownFk extends BaseBScreenView {
    private Display mDisplay;
    private BScreenTitleView mBsTitleView;
    private BScreenRecyclerView mBsRecyclerView;
    private BottomSnView mBsSnView;
    private List<OrderBean> mList = new ArrayList<>();
    private BaseQuickAdapter<OrderBean, BaseViewHolder> mAdapter;
    private Context mContext;
    private Handler mHandler;
    private WaveProgressView waveProgressView;
    private int waveProgress;

    public Handler getHandler() {
        if (mHandler == null) {
            mHandler = new Handler(Looper.getMainLooper());
        }
        return mHandler;
    }

    public BScreenDownFk(Context context) {
        this(context, null);
    }

    public BScreenDownFk(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BScreenDownFk(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.view_down_fk;
    }

    @Override
    protected void initView(View view) {
        waveProgressView = findViewById(R.id.waveProgressView);
        waveProgressView.setProgress(0);

    }

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            //要做的事情，这里再次调用此Runnable对象，以实现每两秒实现一次的定时器操作
            if (waveProgressView.getProgress() < waveProgress) {
                waveProgressView.setProgress(waveProgressView.getProgress() + 1);
            }
            getHandler().postDelayed(this, 100L);
        }
    };

    @Override
    protected void initData() {


    }


    public void setData(Display display, int statusBarHeight, Context context) {
        waveProgress = 0;
        getHandler().postDelayed(runnable, 100L);
        mDisplay = display;
        mContext = context;
        downMainpSbruanDiner();
        show();
    }

    public void downMainpSbruanDiner() {
        waveProgress = 5;
        //下载餐次表
        new SysHttpUtil(mContext).downMainpSbruanDinner(new BlockingBaseObserver<Result<List<SbruanDinnerBean>>>() {
            @Override
            public void onNext(@NonNull Result<List<SbruanDinnerBean>> listResult) {
                waveProgress = 10;

                SbruanDinnerDataManager.getInstance().insertInTxForDelete(listResult.getResult());
                downFk();
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });
    }

    /**
     * 下载FK表
     */
    public void downFk() {
        new SysHttpUtil(mContext).downFk(new BlockingBaseObserver<Result<List<FkBean>>>() {
            @Override
            public void onNext(@NonNull Result<List<FkBean>> listResult) {
                for (FkBean goo : listResult.getResult()) {
                    try {
                        goo.setCardsn(goo.getCardsn().trim());
                    } catch (Exception e) {
                        goo.setCardsn("");
                    }


                }
                FkBeanManager.getInstance().insertInTxForDelete(listResult.getResult());
                waveProgress = 120;
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });
    }


}
